home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / interp / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  3.4 KB  |  135 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: error.c,v 1.6 94/06/27 16:31:46 wlott Exp $
  27. *
  28. * This file implements the stuff to signal errors from C code.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34.  
  35. #include "mindy.h"
  36. #include "str.h"
  37. #include "thread.h"
  38. #include "module.h"
  39. #include "sym.h"
  40. #include "list.h"
  41. #include "vec.h"
  42. #include "type.h"
  43. #include "def.h"
  44. #include "bool.h"
  45. #include "obj.h"
  46. #include "print.h"
  47. #include "func.h"
  48. #include "driver.h"
  49.  
  50. static boolean error_system_enabled = FALSE;
  51.  
  52. static struct variable *error_var = NULL;
  53. static struct variable *type_error_var = NULL;
  54.  
  55. void error(char *msg, ...)
  56. {
  57.     int nargs = count_format_args(msg);
  58.     va_list ap;
  59.     int i;
  60.     struct thread *thread = thread_current();
  61.     
  62.     if (error_system_enabled) {
  63.     *thread->sp++ = error_var->value;
  64.     *thread->sp++ = make_string(msg);
  65.     va_start(ap, msg);
  66.     for (i = 0; i < nargs; i++)
  67.         *thread->sp++ = va_arg(ap, obj_t);
  68.     va_end(ap);
  69.  
  70.     invoke(thread, nargs+1);
  71.     go_on();
  72.     }
  73.     else if (thread) {
  74.     obj_t cond = make_vector(nargs+1, NULL);
  75.  
  76.     SOVEC(cond)->contents[0] = make_string(msg);
  77.     va_start(ap, msg);
  78.     for (i = 1; i <= nargs; i++)
  79.         SOVEC(cond)->contents[i] = va_arg(ap, obj_t);
  80.     va_end(ap);
  81.  
  82.     thread_debuggered(thread, cond);
  83.     }
  84.     else {
  85.     obj_t cond = make_vector(nargs, NULL);
  86.  
  87.     va_start(ap, msg);
  88.     for (i = 0; i < nargs; i++)
  89.         SOVEC(cond)->contents[i] = va_arg(ap, obj_t);
  90.     va_end(ap);
  91.     
  92.     printf("error: ");
  93.     vformat(msg, SOVEC(cond)->contents);
  94.     putchar('\n');
  95.     exit(1);
  96.     }
  97. }
  98.  
  99. void type_error(obj_t value, obj_t type)
  100. {
  101.     if (error_system_enabled) {
  102.     struct thread *thread = thread_current();
  103.     *thread->sp++ = type_error_var->value;
  104.     *thread->sp++ = value;
  105.     *thread->sp++ = type;
  106.     invoke(thread, 2);
  107.     go_on();
  108.     }
  109.     else
  110.     error("%= is not an instance of type %=", value, type);
  111. }
  112.  
  113. void check_type(obj_t thing, obj_t type)
  114. {
  115.     if (!instancep(thing, type))
  116.     type_error(thing, type);
  117. }
  118.  
  119. static obj_t enable_error_system(void)
  120. {
  121.     error_system_enabled = TRUE;
  122.     return obj_True;
  123. }
  124.  
  125. void init_error_functions(void)
  126. {
  127.     define_function("enable-error-system", obj_Nil, FALSE, obj_False, FALSE,
  128.             obj_ObjectClass, enable_error_system);
  129.     error_var = find_variable(module_BuiltinStuff, symbol("error"),
  130.                   FALSE, TRUE);
  131.     type_error_var = find_variable(module_BuiltinStuff, symbol("type-error"),
  132.                    FALSE, TRUE);
  133. }
  134.  
  135.